package com.hsd.commons.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hsd.commons.model.HsdAccount;
import com.hsd.commons.model.HsdAppCodeIndex;
import com.hsd.commons.service.IAuthService;
import com.hsd.core.base.action.BaseAction;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.core.utils.MD5;
import com.hsd.core.utils.TimeUtil;

/**
 * 用户管理
 * @author Thinkpad
 *
 */
@Controller
@RequestMapping("/auth/user")
public class UserManageAction extends BaseAction{
    private static final Logger logger = Logger.getLogger(UserManageAction.class); 
	@Autowired
	private IAuthService authService;
	/**
	 * 系统用户列表
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/loadUserList.do")
	public ModelAndView loadUserList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String loginId = request.getParameter("loginId");
		String userName = request.getParameter("userName");
		Pagination page = authService.loadUserList(pageParams,loginId,userName);
		
		model.addAttribute("page",page);
		model.addAttribute("loginId",loginId);
		model.addAttribute("userName",userName); 
		return new ModelAndView("/app/module/user/userList");
	}
	 
	/**
	 * 新增用户信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOneUser.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneUser(HsdAccount userobj,HttpServletRequest request,ModelMap model){
		logger.debug("enter:" + userobj);
		Map query = new HashMap();
		query.put("loginid", userobj.getLoginid());
		Object existData = authService.getUniqueObject(HsdAccount.class, query);
		if(null != existData){
			return "该帐号已存在，请修改！";
		}
		String password = request.getParameter("password");
		String confirmpwd = request.getParameter("confirmpwd");
		String md5password = MD5.md5(password);
		String md5confirmpassword = MD5.md5(confirmpwd);
		if(!md5password.equals(md5confirmpassword)){
			return "两次密码输入不一致";
		}
		userobj.setPassword(md5password);
 		userobj.setIsuse(1l); 
 		userobj.setStatus(1l);  
		userobj.setId(this.authService.getSeqNextVal("seq_hsd_account_id"));
		userobj.setCreatedate(TimeUtil.getCurrentDateTime());
		HsdAccount accInfo = (HsdAccount)this.getCurrentuser(request);
		if(accInfo != null){
			userobj.setCreateuser(accInfo.getId());
		}
		authService.save(userobj); 
		 
		return "保存成功！";
	}
	/**
	 * 修改用户信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOneUser.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneUser(HsdAccount userobj,HttpServletRequest request,ModelMap model){ 
		//authService.save(userobj);
		return "修改成功！";
	}
	
	 
	/**
	 * 查询用户信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selectOneUser.do")
	public String selectOneDict(HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("id", request.getParameter("id"));
		HsdAccount userobj = (HsdAccount)authService.getUniqueObject(HsdAccount.class, query);
		
		model.addAttribute("userobj",userobj);
		return "app/module/user/update-one-user";
	}
	
	
	/**
	 * 删除用户信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneUser.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneRole(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id"); 
		HsdAccount userObj = (HsdAccount)authService.findByID(HsdAccount.class, new Long(id));
		userObj.setIsuse(0l);
		authService.saveObject(userObj);
		return "删除成功！";
	}
}
